package de.clapp.order;

import android.os.Handler;
import android.widget.Toast;
import de.clapp.CLAppModel;
import de.clapp.R;
import de.clapp.backend.network.ClientRequest;
import de.clapp.backend.network.TCPSocket;
import de.clapp.backend.requests.OrderRequest;
import de.clapp.data.Club;
import de.clapp.data.Order;

public class OrderThread extends Thread
{

    private Order order;
    private Handler handler;
    
    public OrderThread(Order order, Handler handler)
    {
        this.order = order;
        this.handler = handler;
    }

    @Override
    public void run()
    {
        ClientRequest request = null;
        OrderRequest newOrder = new OrderRequest();
        try
        {
            newOrder.setItem(order);
            Club currentClub = CLAppModel.getInstance().getCurrentClub();
            request = new ClientRequest(new TCPSocket(currentClub.getIpAddress(), currentClub.getPort()));
            request.request(newOrder);
            request.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        OrderModel.INSTANCE.emptyOrder();
        handler.post(new Runnable()
        {
            
            @Override
            public void run()
            {
                Toast.makeText(CLAppModel.getInstance().getActivity(), "Ihre Bestellung wird bearbeitet", Toast.LENGTH_LONG);
                CLAppModel.getInstance().getClubMainFragment().setCurrentFragment(R.id.action_club_menu);
            }
        });
    }
    

}
